using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Net;
using System.Runtime ;
using System.Runtime.InteropServices ;

namespace Deloitte
{
    public class WebFunctions
    {
        

    //Creating the extern function...
    [DllImport("wininet.dll")]
    private extern static bool InternetGetConnectedState( out int Description, int ReservedValue ) ;
        
    //Creating a function that uses the API function...
        public static bool IsConnectedToInternet()
        {
            //return true;
            int desc = 0;
            return InternetGetConnectedState(out desc, 0);
        }

        public static void SubmitToDigg(string url, string title, string bodyText, string topic)
        {            
            if (string.IsNullOrEmpty(url)) return;
            if (string.IsNullOrEmpty(title)) title = string.Empty;
            if (string.IsNullOrEmpty(bodyText)) bodyText = string.Empty;
            if (string.IsNullOrEmpty(topic)) bodyText = string.Empty;
            System.Diagnostics.Process.Start(@"http://digg.com/submit?phase=2&url=" + Deloitte.StringHelper.UrlEncode(url) + "&title=" + Deloitte.StringHelper.UrlEncode(title) + "&bodytext=" + Deloitte.StringHelper.UrlEncode(bodyText) + "&topic=" + Deloitte.StringHelper.UrlEncode(topic));
        }
           /// <summary>
        /// Gets the response stream
        /// </summary>
        /// <param name="url">The url of the feed</param>        
        public static System.IO.Stream GetResponseStream(string url)
        {
            return Deloitte.WebFunctions.GetResponseStream(url, string.Empty, string.Empty);
        }
        /// <summary>
        /// Gets the response stream
        /// </summary>
        /// <param name="url">The url of the feed</param>
        /// <param name="userName">The user name required for authentication</param>
        /// <param name="password">Password required for authentication</param>
        public static System.IO.Stream GetResponseStream(string url, string userName, string password)
        {
            try
            {
                 string responseUrl = url;
                
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
                if (userName != string.Empty && password != string.Empty)
                {
                    request.Credentials = new NetworkCredential(userName, password);
                }
                System.IO.Stream responseStream = request.GetResponse().GetResponseStream();// System.Text.ASCIIEncoding.ASCII);                
                return responseStream;
            }
            catch (WebException webEx)
            {
                try
                {
                    if (((HttpWebResponse)webEx.Response).StatusCode == HttpStatusCode.Unauthorized || ((HttpWebResponse)webEx.Response).StatusCode == HttpStatusCode.Forbidden)
                    {
                        if (url.IndexOf("http:") != -1) { return Deloitte.WebFunctions.GetResponseStream(url.Replace("http:", "https:"), userName, password); }
                    }
                }
                catch { throw webEx; }
                throw webEx;
            }
            catch (Exception ex) { throw ex; }
        }
    }
}